﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace HanGu.CRM.Areas.CustomerInfo.Controllers
{
    public class ChangeChargeRecordeController : AdminBaseController
    {
        //
        // GET: /ChangeChargeRecorde/

        public ActionResult Index(int id)
        {
            ViewData["ParentID"] = id;
            return View(GetChangeChargeRecordeList(id));
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            int id = 0;
            int.TryParse(Request["VIPID"], out id);
            return View(GetChangeChargeRecordeList(id));
        }

        List<VChangeChargeRecorde> GetChangeChargeRecordeList(int id)
        {
            SearchParma.IntObj1 = id==0?-1:id;
            SearchParma.Pagination.IsPaging = false;
            List<VChangeChargeRecorde> changeChargeRecordeList = serviceChangeCharge.GetAll(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return changeChargeRecordeList;
        }

        SelectList GetListFromDict(string groupName, bool IsSelectLabel = false)
        {
            var lst = serviceDict.GetByGroupName(groupName);
            if (IsSelectLabel)
            {
                lst.Insert(0, new HanGu.Entity.Dictionary() { key = string.Empty, Value = HelperCommon.SelectDefaultValue });
            }
            SelectList sList = new SelectList(lst, "GenerateKey", "Value");
            return sList;
        }

        [HttpGet]
        public ActionResult Create(int id, int guid = 0)
        {
            //ViewData["Degree"] = GetListFromDict("Degree");
            var model = serviceMemberInfo.GetByID(guid);
           ViewData["MemberInfo"] = TempData["MemberInfo"] = model;
           int charge = 0;
           if (model.Charge.HasValue)
           {
              charge=(int)model.Charge;
           }
           ChangeChargeRecorde ChangeChargeRecorde = new ChangeChargeRecorde() { VIPID = guid, ChangeTime = DateTime.Now, Changer = CurentUser.UserName, SourceValue = charge };
            return View(ChangeChargeRecorde);
        }

        //
        // POST: /ChangeChargeRecorde/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            ChangeChargeRecorde changeChargeRecorde = new ChangeChargeRecorde();
            var memberinfo = TempData["MemberInfo"] as MemberInfo;
            try
            {               
                if (memberinfo != null)
                {
                    TryUpdateModel(changeChargeRecorde, collection);
                    if (serviceChangeCharge.Add(changeChargeRecorde))
                    {
                        memberinfo.Charge = memberinfo.Charge ?? 0;
                        memberinfo.Charge = memberinfo.Charge + changeChargeRecorde.ChangeValue;
                        if (serviceMemberInfo.Update(memberinfo))
                        {
                            //navTabId = "CustomerInfoDefault";
                            ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                        }
                        else
                        { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
            }
            catch (Exception ex)
            {
                TempData["MemberInfo"] = memberinfo;
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }

        //
        // GET: /ChangeChargeRecorde/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {
                serviceChangeCharge.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
